var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images:[],
    uploadId: 0,
    album_id: 0,
    content:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      album_id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  chooseImg:function(){
    var _this = this;
    wx.chooseImage({
      success: function(res) {
        var images = _this.data.images;
        images = images.concat(res.tempFilePaths);
        _this.setData({
          images
        })
      },
    })
  },
  bindContent: function(e){
    this.setData({
      content: e.detail.value
    })
  },
  submit: function(){
    var length = this.data.images.length;
    var _this = this;
    if(length){
      this.uploadImg(0);
    }else{
      wx.showModal({
        content: '请选择上传图片',
        success:function(){
          _this.chooseImg();
        }
      })
    }
  },
  uploadImg: function(i){
    var _this = this;
    var length = this.data.images.length;
    var open_id = wx.getStorageSync('open_id');
    wx.uploadFile({
      url: app.apiHost + 'photo/upload', 
      filePath: _this.data.images[i],
      name: 'file',
      formData: {
        content: _this.data.content,
        id: _this.data.uploadId,
        album_id: _this.data.album_id,
        open_id
      },
      success(res) {
        var data = JSON.parse(res.data);
        _this.setData({
          uploadId: data.data.upload_id
        })
        // const data = res.data
        //do something
      },
      complete(){
        i++;
        if (i == length){
          wx.showToast({
            title: '上传完成',
            duration: 1000,
            success: function(){
              wx.navigateBack({
                
              })
            }
          })
        }else{
          _this.uploadImg(i)
        }
      }
    })
  }
})